package com.baosight.sczx.zm.ca.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.spring.SpringApplicationContext;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.dao.Dao;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.User;
import com.baosight.sczx.common.zm.cInterface.cf.MessageUtil;
import com.baosight.sczx.common.zm.cInterface.cl.RoundCheckWorkNote;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.common.zm.cInterface.cs.WorkSchedule;
import com.baosight.sczx.common.zm.cInterface.cs.WorkingFace;
import com.baosight.sczx.zm.ca.domain.VZMCA4200;
import com.baosight.sczx.zm.ca.domain.ZMCA4500;
import com.baosight.sczx.zm.ca.domain.ZMCA4600;
import com.baosight.sczx.zm.ca.domain.ZMCA4700;
import com.baosight.sczx.zm.ca.domain.ZMCA4800;

public class ServiceZMCA42 extends ServiceEPBase{
    //初始化界面，主要是初始化Combobox
	private static Dao dao = (Dao)SpringApplicationContext.getBean("dao");
	public EiInfo initLoad(EiInfo info)
	{    
		info.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		return query(info);
	}
	
	public  EiBlock initTypeBlockCombobox( )
	{
		EiBlock block = new EiBlock("ProblemType");
		
		EiColumn column = new EiColumn("fdProblemTypeId");
		block.addMeta(column);
		column = new EiColumn("fdProblemType");
		block.addMeta(column);
		
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		Map<String, String> map = new HashMap<String, String>();
		map.put("fdProblemTypeId", "1");
		map.put("fdProblemType", "生产问题");
		list.add(map);
		
		map = new HashMap<String, String>();
		map.put("fdProblemTypeId", "2");
		map.put("fdProblemType", "安全隐患");
		list.add(map);
		
		map = new HashMap<String, String>();
		map.put("fdProblemTypeId", "3");
		map.put("fdProblemType", "安全三违");
		list.add(map);
		
		block.addRows(list);
		return block;
	}
	
	/**
	 * 
	 * <p>Function: addCondition</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-9-6上午07:59:50 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings("unused")
	private void addCondition(EiInfo inInfo) {
		if(inInfo.getBlock("queryBlock")!=null){
			String fdProblemtype = inInfo.getBlock("queryBlock").getCellStr(0, "fdProblemType"); 
			String fdPlace = inInfo.getBlock("queryBlock").getCellStr(0, "fdPlace"); 
			String fdUnit = inInfo.getBlock("queryBlock").getCellStr(0, "fdUnit");
			String fdProblemDesc = inInfo.getBlock("queryBlock").getCellStr(0, "fdProblemDesc");
			
			StaticTools.setQueryParameters(inInfo, "fdProblemTypeId", fdProblemtype, true);
			StaticTools.setQueryParameters(inInfo, "fdPlace", fdPlace, true);
			StaticTools.setQueryParameters(inInfo, "fdUnit", fdUnit, true);
			StaticTools.setQueryParameters(inInfo, "fdProblemDesc", fdProblemDesc, true);
			
			StaticTools.setQueryParameters(inInfo, "fdUserId", UserSession.getLoginName().toString().trim(), true);

		}
		return;
	}
	
	public EiInfo query(EiInfo info)
	{	
		StaticTools.setQueryParameters(info, "fdUserId", UserSession.getLoginName().toString().trim(), true);
		EiInfo outInfo = super.query(info,"ZMCA4200.query",new VZMCA4200());
		outInfo.addBlock(initTypeBlockCombobox());	
		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.cl.orgName", "orgCode", "orgName", false));//单位
		return outInfo;
	}
	/*
	 * ZMCA46
	 */
	public EiInfo initDataZMCA46(EiInfo info)
	{
		info.addBlock("result").set("showCount", "true");
		StaticTools.setQueryParameters(info, "fdKeyId", info.getBlock("inqu").getCell(0, "fdKeyId"), true);
		EiInfo outInfo = super.query(info,"ZMCA4600.query",new ZMCA4600());
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cp.problemType"));//问题类型
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cp.problemLevel"));//问题级别
        outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.cp.department", "orgCode", "orgName", false));//部门
        
        return outInfo;
	}
	/*
	 * ZMCA47
	 */
	public EiInfo initDataZMCA47(EiInfo info)
	{
		info.addBlock("result").set("showCount", "true");
		StaticTools.setQueryParameters(info, "fdKeyId", info.getBlock("inqu").getCell(0, "fdKeyId"), true);
		EiInfo outInfo = super.query(info,"ZMCA4700.query",new ZMCA4700());
        
		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.cl.orgName", "orgCode", "orgName", false));//隐患单位
		outInfo.addBlock(User.getInstance().getConstructionStaff("cmes.cl.empName", "empCode", "empName", true));//人员
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cl.riskSource"));//隐患来源
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cl.riskLevel"));//隐患级别
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cl.riskClassify"));//隐患分类
		outInfo.addBlock(WorkSchedule.getWorkScheduleBlock("cmes.cl.riskWorknum", false));//隐患班次
		outInfo.addBlock(WorkingFace.getAllWorkingFace("cmes.cl.workface", true,UserSession.getInSessionProperty("miningCode").toString()));//所在工作面
		//outInfo.addBlock(RoadWay.getAllRoadWay("cmes.cl.path", true));//所在巷道
		outInfo.addBlock(RoundCheckWorkNote.getAllNoteCode("cmes.cl.workNote", true));//巡检工单
		
		//初始化隐患新增页面
		String riskSource = StaticTools.initCombox(InitialData.getInstance().getEiBlockByNo("cmes.cl.riskSource"), 0);
		outInfo.set("riskSource", riskSource);//默认填入与日常巡检等同的安全巡检
		outInfo.set("manageDepartment", UserSession.getInSessionProperty("orgCode"));//默认填入当前登录用户所在的组织机构

		//初始化罚款页面
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cl.penaltyType"));//罚款分类
		String sendnoteDepartment = UserSession.getInSessionProperty("orgName").toString();
		outInfo.set("sendnoteDepartment", sendnoteDepartment);//开单单位默认填入当前登录人所在部门信息
		outInfo.set("sendnoteDepartmentId", UserSession.getInSessionProperty("orgCode"));
        return outInfo;
	}
	/*
	 * ZMCA48
	 */
	public EiInfo initDataZMCA48(EiInfo info)
	{
		info.addBlock("result").set("showCount", "true");
		StaticTools.setQueryParameters(info, "fdKeyId", info.getBlock("inqu").getCell(0, "fdKeyId"), true);
		EiInfo outInfo = super.query(info,"ZMCA4800.query",new ZMCA4800());
        
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cl.classify"));//三违分类
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cl.sourceClassify"));//三违来源
		outInfo.addBlock( WorkSchedule.getWorkScheduleBlock("cmes.cl.workNum", false));//班次
		outInfo.addBlock(WorkSchedule.getDefaultWorknum());//默认班次
		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.cl.orgName", "orgCode", "orgName", false));//三违单位
		outInfo.addBlock(User.getInstance().getConstructionStaff("cmes.cl.empName", "empCode", "empName", true));//检查人
		outInfo.addBlock(WorkingFace.getAllWorkingFace("cmes.cl.workFace", true,UserSession.getInSessionProperty("miningCode").toString()));//所在工作面
		//outInfo.addBlock(RoadWay.getAllRoadWay("cmes.cl.roadWay", true));//所在巷道
		outInfo.addBlock(RoundCheckWorkNote.getAllNoteCode(null, true));//巡检工单编号
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cl.penaltyType"));//罚款分类
		outInfo.set("orgcode", UserSession.getInSessionProperty("orgCode").toString());//跳转罚款页面开单单位的默认值
        return outInfo;
	}
	
	/*
	 * ZMCA49
	 */
	public EiInfo queryZMCA49(EiInfo info )
	{
	  String fdProblemTypeId = info.getString("fdProblemTypeId");
	  String fdProblemId = info.getString("fdKeyId");
	  StaticTools.setQueryParameters(info, "fdProblemTypeId", fdProblemTypeId, true);
	  StaticTools.setQueryParameters(info, "fdProblemId", fdProblemId, true);
	  EiInfo outInfo = super.query(info, "ZMCA4500.query", new ZMCA4500());
	  outInfo.addBlock(User.getInstance().getConstructionStaff("cmes.cp.people", "empCode", "empName", false));
	  return outInfo ;
	}

	/*
	 * ZMCA45
	 */
	public EiInfo initDataZMCA45(EiInfo info)
	{
		info.addBlock(User.getInstance().getConstructionStaff("cmes.cp.people", "empCode", "empName", false));
		info.set("fdCheckPersonId", UserSession.getLoginName().toString().trim());
		java.text.SimpleDateFormat tempDate = new java.text.SimpleDateFormat("yyyy-MM-dd");
		info.set("fdCheckTime",tempDate.format(new java.util.Date()));
		return info;
	}
	
	/*
	 * saveZMCA45 保存
	 */
	public EiInfo saveZMCA45(EiInfo info)
	{
		if(info.getBlock("saveBlock")!=null)
		{
			String fdKeyId = TablePk.getInstance().getNextValue("T_CF_LEADERAUTHORITY");
			info.getBlock("saveBlock").setCell(0, "fdKeyId", fdKeyId);		
			
			info = super.insert(info,"ZMCA4500.insert","saveBlock");
			String checkContent = info.getBlock("saveBlock").getRow(0).get("fdCheckContent").toString();
			String feedbackPerson = info.getBlock("saveBlock").getRow(0).get("fdFeedbackPersonId").toString();
			MessageUtil messageUtil = new MessageUtil();
			messageUtil.setDao(dao);
			int result = messageUtil.insertIntoMessageCache("领导批示： " + checkContent, "ZMCF30", "", feedbackPerson, 4);
			if(result != 1)
			{
				System.out.println("消息发送失败:code:"+result);
			}

		}
		return info;
	}
}
